package com.kkb.study;

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class LambdaDemo {
    public static void main(String[] args) {
        test08();
    }

    public static void test01(){ //过滤后遍历list
        List<Integer> list = Arrays.asList(1,2,3,4,5);
        list.stream().filter(item -> {
            return item % 2 == 0;
        }).forEach(item -> {
            System.out.println(item);
        });
    };

    public static void test02(){ // 过滤后遍历输出（双冒号运算符）
        List<Integer> list = Arrays.asList(1,2,3,4,5);
        list.parallelStream().filter(item -> {
            return item % 2 == 0;
        }).forEach(System.out::println);
    };

    public static void test03(){ //List过滤输出
        ArrayList<Person> arrayList = new ArrayList<Person>();
        arrayList.add(new Person(1,"tom",28,"男"));
        arrayList.add(new Person(2,"jerry",32,"男"));
        arrayList.add(new Person(3,"jack",35,"男"));
        arrayList.add(new Person(4,"rose",22,"女"));
        arrayList.add(new Person(5,"lilei",18,"男"));
        arrayList.add(new Person(6,"hanmeimei",16,"女"));

//        arrayList.stream().forEach(System.out::println);

//        arrayList.stream().forEach(item -> {
//            System.out.println(item);
//        });
        arrayList.stream().filter(item -> {
            return item.getSex() == "男";
        }).forEach(System.out::println);
    };

    public static void test04(){ // List转Map
        ArrayList<Person> arrayList = new ArrayList<Person>();
        arrayList.add(new Person(1,"tom",28,"男"));
        arrayList.add(new Person(2,"jerry",32,"男"));
        arrayList.add(new Person(3,"jack",35,"男"));
        arrayList.add(new Person(4,"rose",22,"女"));
        arrayList.add(new Person(5,"lilei",18,"男"));
        arrayList.add(new Person(6,"hanmeimei",16,"女"));
        arrayList.add(new Person(6,"xxx",16,"女"));

        Map<Integer,String> map = arrayList.stream().collect(
            Collectors.toMap(Person::getId, item->{
                return item.getName();
            },(k1,k2)->k2) // 如果key值重复，保留后面的value值
        );
        System.out.println(map);
    };

    public static void test05(){ // List分组（分组后返回Map）
        ArrayList<Person> arrayList = new ArrayList<Person>();
        arrayList.add(new Person(1,"tom",28,"男"));
        arrayList.add(new Person(2,"jerry",32,"男"));
        arrayList.add(new Person(3,"jack",35,"男"));
        arrayList.add(new Person(4,"rose",22,"女"));
        arrayList.add(new Person(5,"lilei",18,"男"));
        arrayList.add(new Person(6,"hanmeimei",16,"女"));
        arrayList.add(new Person(6,"xxx",16,"女"));

        Map<String, List<Person>> map = arrayList.stream().collect(
                //Collectors.groupingBy(Person::getSex)
                Collectors.groupingBy(item -> {
                    return item.getSex();
                })
        );
        System.out.println(map);
    };

    public static void test06(){ // 过滤lsit并记录过滤后的list
        ArrayList<Person> arrayList = new ArrayList<Person>();
        arrayList.add(new Person(1,"tom",28,"男"));
        arrayList.add(new Person(2,"jerry",32,"男"));
        arrayList.add(new Person(3,"jack",35,"男"));
        arrayList.add(new Person(4,"rose",22,"女"));
        arrayList.add(new Person(5,"lilei",18,"男"));
        arrayList.add(new Person(6,"hanmeimei",16,"女"));
        arrayList.add(new Person(6,"xxx",16,"女"));

        List<Person> femailList = arrayList.stream().filter(item -> {
            return item.getSex().equals("女");
        }).collect(
            Collectors.toList()
        );
        System.out.println(femailList);
    };

    public static void test07(){// 统计年龄的和
        ArrayList<Person> arrayList = new ArrayList<Person>();
        arrayList.add(new Person(1,"tom",28,"男"));
        arrayList.add(new Person(2,"jerry",32,"男"));
        arrayList.add(new Person(3,"jack",35,"男"));
        arrayList.add(new Person(4,"rose",22,"女"));
        arrayList.add(new Person(5,"lilei",18,"男"));
        arrayList.add(new Person(6,"hanmeimei",16,"女"));

        int totalMaleAge = arrayList.stream().filter(item -> {
            return item.getSex().equals("男");
        }).mapToInt(item -> {
            return item.getAge();
        }).sum();
        System.out.println(totalMaleAge);
    };

    public static void test08(){
        List<Integer> list1 = Arrays.asList(1,2,3,4,5,6);
        List<String> list2 = Arrays.asList("a","ab","abc");

        filterHandle(list1, (item) -> {
            return (Integer)item > 3;
        });
        filterHandle(list2, (item) -> {
            return ((String)item).length() >= 2;
        });

        List list3 = newListHandle(list1, (item) -> {
            return (Integer) item > 3;
        });
        System.out.println(list3);

    };

    public static void filterHandle(List list, Predicate fn){
        list.forEach(item -> {
            if(fn.test(item)){
                System.out.println(item);
            }
        });
    }
    public static List newListHandle(List list, Predicate fn){
        Object collect = list.stream().filter(item -> {
            return fn.test(item);
        }).collect(Collectors.toList());
        return (List)collect;
    }

    public static void test09(){};

    public static void test10(){};

    public static void test11(){};

    public static void test12(){};

    public static void test13(){};

    public static void test14(){};

    public static void test15(){};
}
